package com.cy.JDBC.Impl;

import com.cy.JDBC.JDBCNoticeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Repository
public class JDBCNoticeImpl implements JDBCNoticeDao {
    @Autowired
    private DataSource dataSource;
    @Override
    public List<Map<String, Object>> selectNotice() throws SQLException {
        List<Map<String, Object>> list = new ArrayList<>();
        Connection conn = dataSource.getConnection();
        String sql = "select id,title,content,status,type,createdTime " +
                " from sys_notice where id>=?";
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, 1);
        boolean flag = pstmt.execute();
        ResultSet rs = null;
        if (flag) {
            rs = pstmt.getResultSet();
            ResultSetMetaData rsmd=rs.getMetaData();
            int columnCount=rsmd.getColumnCount();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<>();
                for(int i=1;i<=columnCount;i++){
                    String columnLabel=rsmd.getColumnLabel(i);
                    map.put(columnLabel,rs.getObject(columnLabel));
                }
                list.add(map);
            }
        }
        rs.close();
        pstmt.close();
        conn.close();
        return list;
    }

    @Override
    public int insertNotice(Map<String, Object> map) {


        return 0;
    }
}
